#include"head.h"
int makeWorker(threadPool_t *pThreadPool){
    for(int i =0;i < pThreadPool->threadNum;++i){
        pthread_create(pThreadPool->tidArr+1,NULL,threadFunc,(void*)pThreadPool);
    }
}
void *threadFunc(void *parg){
    threadPool_t *pThreadPool = (threadPool_t *)parg; //地址传递传入传出参数
    while (1){
        task_t *pTask;//新建一个task来接收队首的任务
        pthread_mutex_lock(&pThreadPool->mutex);
        while (pThreadPool->taskQueue.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);//解锁了等一个信号
        }
        puts("GetTask");//来信号了自动上锁
        pTask = pThreadPool->taskQueue.pFront;
        deQueue(&pThreadPool->taskQueue);
        pthread_mutex_unlock(&pThreadPool->mutex);
        printf("netFd = %d\n",pTask->netFd);
        recv(pTask->netFd,pTask->token,98,0);
        childHandEvent(pTask);
    }
}
int childHandEvent(task_t *pTask){
    puts("welcome into childHandEvent!");
    task_t mypTask = *pTask; //接收下来
    int netFd = mypTask.netFd;
    char *token=crypt("chao","$6$uw4F3Kne$");
    puts(mypTask.token);
    puts(token);
    if(strcmp(mypTask.token,token)!=0){
        //密码不对退出
        puts("token错误");
        exit(-1);
    }
    puts("token正确");
    while (1){
        int dataLength;
        bzero(mypTask.buf,sizeof(mypTask.buf));
        recvn(pTask->netFd,&dataLength,sizeof(int));//先收火车头
        int ret = recvn(pTask->netFd,mypTask.buf, dataLength);//再收火车数据
        puts(mypTask.buf);
        if(ret == 0){
            puts("对面客户端断开！");
            break;
        }
        lOrderJudge(&mypTask);
    }
    close(netFd);
    return 0;
}